package com.qyhl.webtv.module_user.share;

import android.Manifest;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.widget.ImageView;
import android.widget.Toast;

import com.qyhl.webtv.basiclib.utils.MPermissionUtils;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.utils.GrayCodeUtil;
import com.qyhl.webtv.commonlib.utils.share.ShareUtils;
import com.qyhl.webtv.module_user.R;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import es.dmoral.toasty.Toasty;

public class UserShareUtil {
    private Activity activity;
    private boolean isDestroy = false;

    public UserShareUtil(Activity activity, int imgSize, ImageView imageView) {
        this.activity = activity;

        int px = StringUtils.dip2px(activity, imgSize);
        String advUrl = CommonUtils.getInstance().getAdvUrl();
        if (StringUtils.isNotEmpty(advUrl)) {
            Bitmap image = CodeUtils.createImage(advUrl.trim(), px, px, null);
            if (imageView != null) {
                imageView.setImageBitmap(image);
            }
        }
    }

    public UserShareUtil(Activity activity, int imgSize, String code, ImageView imageView) {
        this.activity = activity;

        int px = StringUtils.dip2px(activity, imgSize);
        if (StringUtils.isNotEmpty(code)) {
            Bitmap image = CodeUtils.createImage(code.trim(), px, px, null);
            if (imageView != null) {
                imageView.setImageBitmap(image);
            }
        }
    }

    public UserShareUtil(Activity activity, int imgSize, String code, ImageView imageView, boolean isGray) {
        this.activity = activity;

        int px = StringUtils.dip2px(activity, imgSize);
        if (StringUtils.isNotEmpty(code)) {
            Bitmap image;
            if (isGray) {
                image = GrayCodeUtil.createImage(code.trim(), px, px, null);
            } else {
                image = CodeUtils.createImage(code.trim(), px, px, null);
            }

            if (imageView != null) {
                imageView.setImageBitmap(image);
            }
        }
    }

    public void sendSMS() {
        if (!isDestroy) {
            String userName = CommonUtils.getInstance().getUserName();
            ShareUtils.getInstance().getShareUrl(userName, 21, new ShareUtils.ShareCallBack() {
                @Override
                public void getUrlCallBack(boolean isSuccess, String jsonShare) {
                    if (!isSuccess) {
                        Toasty.normal(activity, "当前分享链接异常！", Toast.LENGTH_SHORT).show();
                    } else {
                        Uri smsToUri = Uri.parse("smsto:");
                        Intent sendIntent = new Intent(Intent.ACTION_VIEW, smsToUri);
                        //sendIntent.putExtra("address", "123456"); // 电话号码，这行去掉的话，默认就没有电话
                        sendIntent.putExtra("sms_body", "身边人，身边事，本地人自己的手机APP!\n" +
                                "立刻点击下载，来\"" + activity.getString(R.string.app_name) + "\"找我\n" + jsonShare);
                        sendIntent.setType("vnd.android-dir/mms-sms");
                        activity.startActivityForResult(sendIntent, 1002);
                    }
                }
            });
        }
    }

    public void share(final String type) {
        MPermissionUtils.requestPermissionsResult(activity, 1, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new MPermissionUtils.OnPermissionListener() {
            @Override
            public void onPermissionGranted() {
                if (!isDestroy) {
                    String userName = CommonUtils.getInstance().getUserName();
                    ShareUtils.getInstance().goSingleShare(
                            type,
                            userName,
                            21,
                            "立刻点击下载，来\"" + activity.getString(R.string.app_name) + "\"找我",
                            "身边人，身边事，本地人自己的手机APP!",
                            null,
                            activity);
                }
            }

            @Override
            public void onPermissionDenied() {
                if (!isDestroy) {
                    MPermissionUtils.showTipsDialog(activity);
                }
            }
        });
    }

    public void clipBoard() {
        String code = CommonUtils.getInstance().getCode();
        ClipboardManager cm = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData mClipData = ClipData.newPlainText("share_code_copy", code);
        cm.setPrimaryClip(mClipData);
        Toast.makeText(activity, "复制成功！", Toast.LENGTH_SHORT).show();
    }

    public void onDestroy() {
        isDestroy = true;
    }
}
